﻿using Payroll.Biz.Legacy.DAL;
using Payroll.Biz.Legacy.Models;
using Payroll.Biz.Legacy.ServiceParams;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Payroll.Biz.Legacy.Services
{
    public class AddUnionMemberService : AbstractService, IService<AddUnionMemberServiceParams>
    {
        public AddUnionMemberService(IPayrollDatabase database) : base(database)
        {
        }

        public ServiceResult Execute(AddUnionMemberServiceParams serviceParams)
        {
            Employee employee = Database.GetEmployee(serviceParams.EmployeeId);
            if (employee == null)
            {
                throw CreateServiceException("No such employee.");
            }

            if (!(employee.Affiliation is NoAffiliation))
            {
                throw CreateServiceException("The employee has already had union affiliation");
            }

            Guid memberId = Guid.NewGuid();

            Database.AddUnionMember(memberId, employee);

            return ServiceResult.CreateSuccessResult(memberId);
        }
    }
}
